<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">移库单号</label>
        <el-input v-model.trim="query.moveWareHouseOrderNo" clearable placeholder="移库单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">单据类型</label>
        <el-select v-model="query.moveWareHouseOrderType" style="width: 185px;" class="filter-item" clearable placeholder="请选择" @change="crud.toQuery">
          <el-option
            v-for="item in dict.moveOrder_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">移库单状态</label>
        <el-select v-model="query.status" style="width: 185px;" class="filter-item" clearable placeholder="请选择" @change="crud.toQuery">
          <el-option
            v-for="item in dict.allOrder_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column prop="moveWareHouseOrderNo" label="移库单号" />
        <el-table-column prop="moveWareHouseOrderType" label="单据类型">
          <template slot-scope="scope">
            {{ dict.label.moveOrder_type[scope.row.moveWareHouseOrderType] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="移库单状态">
          <template slot-scope="scope">
            {{ dict.label.allOrder_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="parentPalletCodes" label="移库的母托编码（逗号分割）" />
        <el-table-column prop="moveTo" label="目标库区类型" />
        <el-table-column prop="combinedToParentPallet" label="目标母托编码" />
        <el-table-column prop="applicantEmployeeNo" label="申请人工号" />
        <el-table-column prop="applicantName" label="申请人姓名" />
        <el-table-column prop="moveTime" label="移库时间" />
        <el-table-column prop="finishTime" label="完成时间" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="remarks" label="备注" />
        <el-table-column label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;gap: 5px;">
              <el-button type="text" size="mini" @click="checkDetail(scope.row)">查看详情</el-button>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将取消该移库单,是否继续?"
                @confirm="cancelOrder(scope.row)"
              >
                <el-button slot="reference" :disabled="scope.row.status === 3" type="danger" size="mini" :loading="btn.cancel">取消移库</el-button>
              </el-popconfirm>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将执行该移库单,是否继续?"
                @confirm="excuteOrder(scope.row)"
              >
                <el-button slot="reference" :disabled="scope.row.status === 3" type="primary" size="mini" :loading="btn.excute">执行移库单</el-button>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMoveWareHouseOrder from '@/api/work/moveOrder'
import { cancelOrderAPI, excuteOrderAPI } from '@/api/work/moveOrder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, moveWareHouseOrderNo: null, moveWareHouseOrderType: null, status: null, priority: null, parentPalletCodes: null, moveTo: null, combinedToParentPallet: null, applicantEmployeeNo: null, applicantName: null, moveTime: null, finishTime: null, updateTime: null, createTime: null, remarks: null }
export default {
  name: 'MoveOrder',
  components: { pagination, rrOperation, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['moveOrder_type', 'allOrder_status'],
  cruds() {
    return CRUD({ title: 'moveWareHouseOrder', url: 'api/moveWareHouseOrder', idField: 'id', sort: 'id,desc', crudMethod: { ...crudMoveWareHouseOrder },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'moveWareHouseOrder:add', 'otherUser'],
        edit: ['admin', 'moveWareHouseOrder:edit', 'otherUser'],
        del: ['admin', 'moveWareHouseOrder:del', 'otherUser']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'moveWareHouseOrderNo', display_name: '移库单号' },
        { key: 'moveWareHouseOrderType', display_name: '单据类型' },
        { key: 'status', display_name: '移库单状态' }
      ],
      btn: {
        cancel: false,
        excute: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    checkDetail(row) {
      this.$router.push(`/moveManagement/moveOrderDetail?id=${row.id}`)
    },
    async cancelOrder(row) {
      this.btn.cancel = true
      try {
        const res = await cancelOrderAPI(row.moveWareHouseOrderNo)
        if (res.result === 'OK') {
          this.$message.success('取消移库成功')
          this.crud.refresh()
        } else {
          return this.$message.warning(res.data)
        }
      } finally {
        this.btn.cancel = false
      }
    },
    async excuteOrder(row) {
      this.btn.excute = true
      try {
        const res = await excuteOrderAPI(row.moveWareHouseOrderNo)
        if (res.result === 'OK') {
          this.$message.success('执行移库单成功')
          this.crud.refresh()
        } else {
          return this.$message.warning(res.data)
        }
      } finally {
        this.btn.excute = false
      }
    }
  }
}
</script>

<style scoped>

</style>
